package org.reference.generic.work;

import java.util.ArrayList;
import java.util.List;

import org.reference.generic.base.Item;
import org.reference.generic.base.ReferenceParameter;

import util.Log;

/**
 * 
 */
public class Machine extends Item {

    /**
     * 
     */
	private static int guuid = 0;
	private int uuid=0;
	
    public Machine() {
    	
    //synchronized (this) { //하나의 쓰레드만 접근 가능  
			uuid=guuid++;
		//}
    	release();
    }

    private long endTime;
    private long breakTime;
    private boolean busy=true;
    private double readyTime;
    private double totalWorkingTime;
    private int  step;
    private List<Job> machineSchedule = new ArrayList<Job>();
    private int index;
   	/**
     * 
     */
    private OnCompleteListenner onCompleteListenner;
    /**
     * @param onCompleteListenner
     */
    public void setOnCompleteListenner(OnCompleteListenner onCompleteListenner) {
        this.onCompleteListenner = onCompleteListenner;
    }

    /**
     * @return
     */
    public boolean isBusy() {
   
    	return busy;
    }

    /**
     * @param readyTime
     */
    public void setReadyTime(double readyTime) {
        this.readyTime=readyTime;
    }

    /**
     * @return
     */
    public double getTotalWorkingTime() {
        return totalWorkingTime;
    }

    /**
     * @param step
     */
    public void setStep(int step) {
        this.step = step;
    }

    /**
     * @return
     */
    public int getStep() {
        return step;
    }

    /**
     * @param job
     */
    public void run (Job job) {
    	
    	busy = true;
    	machineSchedule.add(job);
		setReadyTime(job.getProcessingTime());  //machine ready time에 job processing time 집어 넣음.
    }
    
    /**
     * 
     */
    public void onComplete() {
    	busy = false;
    	totalWorkingTime += readyTime;
    	
    	//setValue(String.valueOf(totalWorkingTime));
    	if (onCompleteListenner == null) return;
    	onCompleteListenner.onComplete(this);  // 머신 매니저의 onComplete 호출 	
    }

	/**
     * 
     */
    public interface OnCompleteListenner {
        /**
         * @param machine
         */
        public abstract void onComplete(Machine machine);

    }

	@Override
	public void release() {
    	busy = false;
    	readyTime = 0;
    	step = 0;
    	totalWorkingTime = 0;
    	setName("MACHINE");
    	setValue(String.valueOf(totalWorkingTime));
		super.release();
	}

//	public int getMachineNumber() {
//		return uuid;
//	}

	public double getReadyTime() {
		return readyTime;
	}

	public int getIndex() {  //머신 번호 
		return index;
	}

	public void setIndex(int index) {
		this.index = index;
	}

	public List<Job> getMachineSchedule() {
		return machineSchedule;
	}

	public void setMachineSchedule(List<Job> machineSchedule) {
		this.machineSchedule = machineSchedule;
	}

	public void setBusy(boolean busy) {
		this.busy = busy;
	}
}